package com.five.dao.impl.backstage;

import com.five.dao.backstage.ChuKuDao;
import com.five.entity.backstage.TChukudetail;
import com.five.util.DBUtil;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: jianglin
 * Date: 2020/11/30  15:35
 */
@Repository
public class ChuKuDaoImpl implements ChuKuDao {
    //查询出库记录显示所有数据
    @Override
    public List<Map> queryChuKu(Integer page, Integer limit, TChukudetail tChukudetail) {
        String sql="select x.chuid,x.totalprice,x.totalcount,y.id,y.name as name,x.jsr,x.cdate from t_chuku x,t_staff y where y.id=x.uid";
        if (tChukudetail.getName()!=null){
            sql+=" and name like '%"+tChukudetail.getName()+"%' ";
        }
        if (tChukudetail.getJsr()!=null){
            sql+=" and jsr like '%"+tChukudetail.getJsr()+"%' ";
        }

        if (tChukudetail.getKdata()!="" && tChukudetail.getKdata()!=null && tChukudetail.getEdata()!="" && tChukudetail.getEdata()!=null){
            sql+=" and cdate>'"+tChukudetail.getKdata()+"' and cdate<'"+tChukudetail.getEdata()+"' ";
        }

        sql+=" limit ?,?";
        return DBUtil.queryMap(sql,(page-1)*limit,limit);
    }

    @Override
    public int addchuKu(Map map) {
        String sql="insert into t_chuku (cdate,uid,totalcount,totalprice,jsr) values (now(),?,?,?,?)";
        return DBUtil.getKey(sql,map.get("uid"),map.get("totalcount"),map.get("totalprice"),map.get("jsr"));
    }

    @Override
    public int addChuK(Map map1, int i) {
        String sql="insert into t_chukudetail(ssid,count,chukuid)values(?,?,?)";
        return DBUtil.manage(sql,map1.get("facid"),map1.get("count"),i);
    }

    @Override
    public int addChuKus(Map map1) {
        String sql="update t_facility set count=count-? where id=? ";
        return DBUtil.manage(sql,map1.get("count"),map1.get("facid"));
    }

    @Override
    public List<Map> queryChuId(Integer chukuid) {
        String sql="SELECT se.id id,cd.id facid,se.name facname,tp.name typename,cu.chuid chuid,se.gys gys,cd.count count from t_chukudetail cd,t_chuku cu,t_facility se,t_ptype tp WHERE cd.chukuid=cu.chuid AND cd.ssid=se.id AND se.typeid=tp.id AND cu.chuid=?";
        return DBUtil.queryMap(sql,chukuid);
    }
//分页
    @Override
    public int count(TChukudetail tChukudetail) {
        String sql="SELECT COUNT(*) a FROM t_chuku x,t_staff y where y.id=x.uid";
        if (tChukudetail.getName()!=null){
            sql+=" and name like '%"+tChukudetail.getName()+"%' ";
        }
        if (tChukudetail.getJsr()!=null){
            sql+=" and jsr like '%"+tChukudetail.getJsr()+"%' ";
        }

        if (tChukudetail.getKdata()!="" && tChukudetail.getKdata()!=null && tChukudetail.getEdata()!="" && tChukudetail.getEdata()!=null){
            sql+=" and cdate>'"+tChukudetail.getKdata()+"' and cdate<'"+tChukudetail.getEdata()+"' ";
        }
        List<Map> maps = DBUtil.queryMap(sql);
        String a = maps.get(0).get("a") + "";
        int i = Integer.parseInt(a);
        return i;
    }
//通过获取房间号来对打扫时对对设施数量的修改
    @Override
    public int clean(String roomcode) {
        String sqll="update t_room set status=2 where roomcode=?";
        int ii = DBUtil.manage(sqll, roomcode);
        String sql="SELECT  typeid from t_room where roomcode=?";
        List<Map> maps = DBUtil.queryMap(sql, roomcode);
        String a=  maps.get(0).get("typeid")+"";
        int i = Integer.parseInt(a);
        int i1=0;
        switch (i){
            case 2:
            case 4:
            case 5:
                String sql1="update t_facility set count=count-1 where typeid=1";
                String sql2="insert into t_chuku (cdate,uid,totalcount,totalprice,jsr) values (now(),6,(SELECT count(*) FROM t_facility WHERE typeid=1),(SELECT sum(pmoney) FROM t_facility WHERE typeid=1),'cleaner')";
                 i1 = DBUtil.manage(sql1);
                int i2 = DBUtil.getKey(sql2);
                String sql3="insert into t_chukudetail(ssid,count,chukuid)values(7,1,?),(8,1,?),(9,1,?),(16,1,?)";
                DBUtil.manage(sql3,i2,i2,i2,i2);
                break;
            case 1:
            case 6:
                String sql4="update t_facility set count=count-2 where typeid=1";
                String sql5="insert into t_chuku(cdate,uid,totalcount,totalprice,jsr) values (now(),6,(SELECT count(*)*2 FROM t_facility WHERE typeid=1),(SELECT sum(pmoney)*2 FROM t_facility WHERE typeid=1),'cleaner')";
                i1 = DBUtil.manage(sql4);
                int i3 = DBUtil.getKey(sql5);
                String sql6="insert into t_chukudetail(ssid,count,chukuid)values(7,2,?),(8,2,?),(9,2,?),(16,2,?)";
                DBUtil.manage(sql6,i3,i3,i3,i3);
                break;
            case 3:
                String sql7="update t_facility set count=count-3 where typeid=1";
                String sql8="insert into t_chuku (cdate,uid,totalcount,totalprice,jsr) values (now(),6,(SELECT count(*)*3 FROM t_facility WHERE typeid=1),(SELECT sum(pmoney)*3 FROM t_facility WHERE typeid=1),'cleaner')";
                i1 = DBUtil.manage(sql7);
                int i4 = DBUtil.getKey(sql8);
                String sql9="insert into t_chukudetail(ssid,count,chukuid)values(7,3,?),(8,3,?),(9,3,?),(16,3,?)";
                DBUtil.manage(sql9,i4,i4,i4,i4);
                break;
        }
        return ii+i1;
    }


}
